package com.goldgov.system.web;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.goldgov.kcloud.core.json.JsonQueryObject;
import com.goldgov.system.service.LinuxSystemService;
import com.goldgov.system.service.SystemInfo;
import com.goldgov.system.service.SystemInfoQuery;
import com.goldgov.utils.UrlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/system")
@Api("查询服务器状态")
@Component
@EnableScheduling
public class LinuxSystemController {
	
	protected final Log logger = LogFactory.getLog(this.getClass());
	
	@Autowired
	private LinuxSystemService linuxSystemService;
	
	@GetMapping("/findMemInfo")
	@ApiOperation(value = "查询内存情况", notes ="")
	public String findMemInfo() throws IOException{
		return Jsoup.connect(UrlUtil.SYSTEM_INFO_MEM_URL).ignoreContentType(true).execute().body();
	}
	
	@GetMapping("/findCpuInfo")
	@ApiOperation(value = "查询CPU情况", notes ="")
	public String findCpuInfo() throws IOException{
		return Jsoup.connect(UrlUtil.SYSTEM_INFO_CPU_URL).ignoreContentType(true).execute().body();
	}
	
	@GetMapping("/findInfo")
	@ApiOperation(value = "查询CPU,内存情况", notes ="")
	public String findInfo() throws IOException{
		JSONObject json=new JSONObject();
		json.put("cpu", Jsoup.connect(UrlUtil.SYSTEM_INFO_CPU_URL).ignoreContentType(true).execute().body());
		json.put("mem", Jsoup.connect(UrlUtil.SYSTEM_INFO_MEM_URL).ignoreContentType(true).execute().body());
		return json.toJSONString();
	}
	
	@GetMapping("/findInfoList")
	@ApiOperation(value = "查询情况")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "searchStartDate", value = "开始时间", paramType = "query"),
		@ApiImplicitParam(name = "searchEndDate", value = "结束时间", paramType = "query")
	})
	public JsonQueryObject<SystemInfo> findInfoList(@ApiIgnore SystemInfoQuery query,HttpServletRequest request) throws IOException{
		query.setPageSize(-1);
		query.setResultList(linuxSystemService.findInfoList(query));
		return new JsonQueryObject<>(query);
	}
	
	@Lazy(false)
	@Scheduled(cron = "${task.cron.autoWriteSystemInfo}")
	public void addSystemInfo() {
		try {
			String mem=Jsoup.connect(UrlUtil.SYSTEM_INFO_MEM_URL).ignoreContentType(true).execute().body();
			String cpu=Jsoup.connect(UrlUtil.SYSTEM_INFO_CPU_URL).ignoreContentType(true).execute().body();
			SystemInfo info=new SystemInfo();
			info.setCpu(cpu);
			info.setMem(mem);
			//入库
			linuxSystemService.addSystemInfo(info);
		}catch(Exception e) {
			logger.error("查询内存cpu报错",e);
		}
	}
}
